﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using MyDMS.DAL;
using ListItem = ExtAspNet.ListItem;

namespace MyDMS_ExtJS.Admin
{
    public partial class TaskEdit : System.Web.UI.Page
    {
        private int _Task_Code = 0;
        MyDMSEntity _myDmsEntity = new MyDMSEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                _Task_Code = Convert.ToInt32(Request.QueryString["id"]);
            }
            if (!IsPostBack)
            {
                BindDepartment();
                var sf = _myDmsEntity.Task.FirstOrDefault(s => s.Task_Code == _Task_Code);
                if (sf != null)
                {
                    var project = _myDmsEntity.Project.FirstOrDefault(p => p.Project_Code == sf.Project_Code);
                    BindProject(project.Department_Code);
                    BindAssgin(project.Department_Code);
                    ddlDepartment.SelectedValue = project.Department_Code.ToString();
                    tbName.Text = sf.Task_Title;
                    tbDesc.Text = sf.Task_Desc;
                    ddlProject.SelectedValue = sf.Project_Code.ToString();
                    ddlAssgin.SelectedValue = sf.Assigned_To.ToString();
                }
                else
                {
                    BindProject();
                    BindAssgin();
                }
            }
            if (_Task_Code != 0)
            {
                ddlDepartment.Enabled = false;
            }
        }

        private void BindProject(int departmentCode = 0)
        {
            if (departmentCode != 0)
            {
                ddlProject.DataSource = _myDmsEntity.Project.Where(p => p.Department_Code == departmentCode).ToList();
            }
            else
            {
                ddlProject.DataSource = _myDmsEntity.Project.ToList();
            }
            ddlProject.DataValueField = "Project_Code";
            ddlProject.DataTextField = "Project_Title";
            ddlProject.DataBind();
            ddlProject.Items.Insert(0, new ListItem("选择中项", "-1"));
        }

        private void BindAssgin(int departmentCode = 0)
        {
            if (departmentCode != 0)
            {
                ddlAssgin.DataSource = from uu in _myDmsEntity.User.Where(u => u.Department_Code == departmentCode)
                                       select new
                                                  {
                                                      UserCode = uu.User_Code,
                                                      UserName = uu.FirstName + uu.LastName
                                                  };
            }
            else
            {
                ddlAssgin.DataSource = from uu in _myDmsEntity.User
                                       select new
                                       {
                                           UserCode = uu.User_Code,
                                           UserName = uu.FirstName + uu.LastName
                                       };
            }
            ddlAssgin.DataTextField = "UserName";
            ddlAssgin.DataValueField = "UserCode";
            ddlAssgin.DataBind();
            ddlAssgin.Items.Insert(0, new ListItem("选择负责人", "-1"));
        }

        private void BindDepartment()
        {
            ddlDepartment.DataSource = _myDmsEntity.Department.ToList();
            ddlDepartment.DataTextField = "Department_Name";
            ddlDepartment.DataValueField = "Department_Code";
            ddlDepartment.DataBind();
            ddlDepartment.Items.Insert(0, new ListItem("选择科室", "-1"));
        }

        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            if (_Task_Code != 0)
            {
                var sf = _myDmsEntity.Task.FirstOrDefault(s => s.Task_Code == _Task_Code);
                if (sf != null)
                {
                    sf.Task_Title = tbName.Text;
                    sf.Task_Desc = tbDesc.Text;
                    sf.Updation_Date = DateTime.Now;
                    sf.Assigned_To = Convert.ToInt32(ddlAssgin.SelectedValue);
                    sf.Project_Code = Convert.ToInt32(ddlProject.SelectedValue);
                }
                _myDmsEntity.SaveChanges();
            }
            else
            {
                _myDmsEntity.Task.AddObject(new MyDMS.DAL.Task()
                {
                    Task_Title = tbName.Text,
                    Task_Desc = tbDesc.Text,
                    Updation_Date = DateTime.Now,
                    Creation_Date = DateTime.Now,
                    Creation_By = 0,
                    Assigned_To = Convert.ToInt32(ddlAssgin.SelectedValue),
                    Project_Code = Convert.ToInt32(ddlProject.SelectedValue)
                });
                _myDmsEntity.SaveChanges();
            }

            // 2. 关闭本窗体，然后刷新父窗体
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }

        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProject.Items.Clear();
            BindProject(Convert.ToInt32(ddlDepartment.SelectedValue));
            ddlAssgin.Items.Clear();
            BindAssgin(Convert.ToInt32(ddlDepartment.SelectedValue));
        }
    }
}